package nc.tb.app.plantable.ui;

import java.awt.BorderLayout;

import nc.tb.cube.CubeView;
import nc.tb.cube.model.Plan;
import nc.tb.pub.util.NtbLogger;
import nc.tb.report.ViewUIModel;
import nc.tb.ufo.ui.IMode;
import nc.tb.ufo.ui.UFOTablePane;
import nc.vo.pub.BusinessException;

public class NtbTablePane extends UFOTablePane /*implements IPlanCheckListener*/ {
	
	private NtbPlanTablePanel m_planTablePane = null;
	private Plan m_plan = null;
	private CubeView m_cubeView = null;
	private int nMode = IMode.PLAN;
	
	public NtbTablePane(Plan plan, CubeView cbView) {
		super();
		try {
			setPlan(plan, cbView);
		} catch (Exception e) {
			NtbLogger.printException(e);
		}
	}

	public void setPlan(Plan plan, CubeView cbView) throws Exception {
		setPlan(plan, cbView, IMode.PLAN);
	}
	public void setPlan(Plan plan, CubeView cbView, int nMode) throws Exception {
		m_plan = plan;
		m_cubeView = cbView;
		this.nMode = nMode;
		m_plan.addPlanCheckListener(this);
		add(getNtbPlanTablePanel(), BorderLayout.CENTER);
		revalidate();
		repaint();
	}
//	public void startEditing() throws Exception {
//		System.out.println("StartEditing..." + isEditing());
//		if (getPlan() != null && getPlan().getPrimaryKey() != null) {
//			if (getPlan() instanceof CommonPlan) {
//				getPlan().checkOutByCubeView(getNtbPlanTablePanel().getCubeView().getPrimaryKey());
//				// throw new NotImplementException();
//			} else {
//				getPlan().checkOut();
//			}
//		}
//		System.out.println("StartEditing Complete." + isEditing());
//	}
	public void stopEditing() throws BusinessException {
		getNtbPlanTablePanel().getNtbPlanEditTable().stopEditing();
//		reCalculateSsvo();
	}
	public void cancelEditing() throws Exception {
		NtbLogger.print4("CancelEditing..." + isEditing());
		getPlan().undoCheckOut();
		NtbLogger.print4("CancelEditing Complete." + isEditing());
	}
//	public void cellDataChanged() {
//		repaintPane();
//	}
	public Plan getPlan() {
		return getNtbPlanTablePanel().getPlan();
	}
	public CubeView getCubeView() {
		return m_cubeView;
	}
	public void reCalculateDimVector() throws BusinessException {
//		getReport().getViewUIModel().recaculateDimVector();
	}
	public void repaintPane() {
		getNtbPlanTablePanel().getNtbPlanEditTable().tableRepaint(false);
//		revalidate();
//		getViewUIModel().dataCellDataChanged();
//		getReport().getTable().repaint();
	}
	public void setMode(int mode) {
		getNtbPlanTablePanel().setMode(mode);
	}
	public int getMode() {
		return getNtbPlanTablePanel().getMode();
	}
	
	private NtbPlanTablePanel getNtbPlanTablePanel() {
		if (m_planTablePane == null) {
			m_planTablePane = new NtbPlanTablePanel(m_plan, m_cubeView, nMode);
		}
		return m_planTablePane;
	}
	
	private void setEditable(boolean b) {
		getNtbPlanTablePanel().setEditable(b);
	}
	
	public void planCheckOut() {
		setEditable(true);
	}
	public void planCheckIn() {
		// TODO save plan
		setEditable(false);
	}
	public void planUndoCheckOut() {
		// TODO reload plan
		setEditable(false);
	}
	public void cellDataChanged(boolean isReloadDataCell) {
		if (isReloadDataCell) {
			getNtbPlanTablePanel().getNtbPlanEditTable().tableRepaint(true);
		}
		else {
			repaintPane();
		}
	}
	
	public void onClose() {
		if (m_plan != null)
			m_plan.removePlanCheckListener(this);
	}
	
	public ViewUIModel getViewUIModel() {
		return getNtbPlanTablePanel().getNtbPlanEditTable().getOlapViewTableModel().getViewUIModel();
	}
	
}
